package com.companyName.sample.service.business;

import com.companyName.sample.bean.dto.sys.RoleDto;
import com.companyName.sample.bean.dto.sys.RoleResourceDto;
import com.companyName.sample.bean.dto.sys.Tree;
import com.companyName.sample.common.exception.BusinessException;
import com.companyName.sample.common.util.CommUtil;
import com.companyName.sample.common.util.PageInfo;
import com.companyName.sample.data.entity.RolePo;
import com.companyName.sample.data.entity.RoleResourcePo;
import com.companyName.sample.data.mapper.RoleMapper;
import com.companyName.sample.data.mapper.RoleResourceMapper;
import com.companyName.sample.data.mapper.UserRoleMapper;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by guo.jun on 2016/4/11.
 */
@Component
public class RoleBiz {

    private static Logger logger = LoggerFactory.getLogger(RoleBiz.class);

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;


    public void findDataGrid(PageInfo pageInfo) {
        pageInfo.setRows(roleMapper.findRolePageCondition(pageInfo));
        pageInfo.setTotal(roleMapper.findRolePageCount(pageInfo));
    }


    public List<Tree> findTree() {
        List<Tree> trees = Lists.newArrayList();
        List<RolePo> roles = roleMapper.findRoleAll();
        for (RolePo role : roles) {
            Tree tree = new Tree();
            tree.setId(role.getId());
            tree.setText(role.getName());

            trees.add(tree);
        }
        return trees;
    }


    public int addRole(RolePo role) {
        return roleMapper.insert(role);
    }


    public int deleteRoleById(Long id) {
        return roleMapper.deleteRoleById(id);
    }


    public RoleDto findRoleById(Long id) {
        RolePo rolePo = roleMapper.findRoleById(id);
        RoleDto roleDto = new RoleDto();
        CommUtil.copyProperties(rolePo, roleDto);
        return roleDto;
    }


    public int updateRole(RolePo role) {
        return roleMapper.updateRole(role);
    }


    public List<Long> findResourceIdListByRoleId(Long id) {
        return roleMapper.findResourceIdListByRoleId(id);
    }


    public void updateRoleResource(Long id, String resourceIds) {
        // 先删除后添加,有点爆力
        List<Long> roleResourceIdList = roleMapper.findRoleResourceIdListByRoleId(id);
        if (roleResourceIdList != null && (!roleResourceIdList.isEmpty())) {
            for (Long roleResourceId : roleResourceIdList) {
                roleResourceMapper.deleteById(roleResourceId);
            }
        }
        String[] resources = resourceIds.split(",");
        RoleResourcePo roleResourcePo = new RoleResourcePo();
        for (String string : resources) {
            roleResourcePo.setRoleId(id);
            roleResourcePo.setResourceId(Long.parseLong(string));
            roleResourceMapper.insert(roleResourcePo);
        }
    }


    public List<Long> findRoleIdListByUserId(Long userId) {
        return userRoleMapper.findRoleIdListByUserId(userId);
    }


    public List<Map<Long, String>> findRoleResourceListByRoleId(Long roleId) {
        return roleMapper.findRoleResourceListByRoleId(roleId);
    }

}
